/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement.Components;

import Utilities.Constants.StorageDeviceAction;
import Utilities.Functions;
import org.virtualbox_3_2.DeviceType;

/**
 * This class represents the settings of a device on a virtual machine.
 *
 * @author Angel Sanadinov
 */
public class DeviceSettings extends MachineManagementComponent
{
    private String controllerName;  //the name of the device's controller
    private int controllerPort;     //the device's controller port
    private int controllerSlot;     //the device's controller slot
    private DeviceType device;      //the type of the device
    private String mediumId;        //the medium id of the device, if available
    private StorageDeviceAction deviceAction; //device action to carry out

    /**
     * Constructs a device settings object with the specified data. <br><br>
     *
     * Usually used when either a medium is not needed or is not available.
     *
     * @param controllerName the name of the device's controller
     * @param controllerPort the device's controller port
     * @param controllerSlot the device's controller slot
     * @param device the type of the device
     *
     * @see StorageDeviceAction#INVALID_ACTION
     */
    public DeviceSettings(String controllerName, int controllerPort, int controllerSlot, DeviceType device)
    {
        this.controllerName = controllerName;
        this.controllerPort = controllerPort;
        this.controllerSlot = controllerSlot;
        this.device = device;
        this.deviceAction = StorageDeviceAction.INVALID_ACTION;
        this.mediumId = null;
    }

    /**
     * Constructs a device settings object with the specified data. <br><br>
     *
     * Usually used when a medium is necessary.
     *
     * @param controllerName the name of the device's controller
     * @param controllerPort the device's controller port
     * @param controllerSlot the device's controller slot
     * @param device the type of the device
     * @param mediumId the id of the medium
     *
     * @see StorageDeviceAction#INVALID_ACTION
     */
    public DeviceSettings(String controllerName, int controllerPort, int controllerSlot, DeviceType device, String mediumId)
    {
        this(controllerName, controllerPort, controllerSlot, device);
        this.mediumId = mediumId;
    }

    /**
     * Retrieves the device's controller name.
     *
     * @return the controller name
     */
    public String getControllerName()
    {
        return controllerName;
    }

    /**
     * Retrieves the device's controller port.
     *
     * @return the controller port
     */
    public int getControllerPort()
    {
        return controllerPort;
    }

    /**
     * Retrieves the device's controller slot.
     *
     * @return the controller slot
     */
    public int getControllerSlot()
    {
        return controllerSlot;
    }

    /**
     * Retrieves the device's type.
     *
     * @return the device type
     */
    public DeviceType getDevice()
    {
        return device;
    }

    /**
     * Retrieves the id of the medium associated with this device.
     *
     * @return the medium id
     */
    public String getMediumId()
    {
        return mediumId;
    }

    /**
     * Retrieves the action to be carried out for this device.
     *
     * @return the device action
     */
    public StorageDeviceAction getDeviceAction()
    {
        return deviceAction;
    }

    /**
     * Sets the medium id for the device.
     *
     * @param mediumId the medium id
     */
    public void setMediumId(String mediumId)
    {
        this.mediumId = mediumId;
    }

    /**
     * Sets the action to be carried out for this device.
     *
     * @param deviceAction the device action
     */
    public void setDeviceAction(StorageDeviceAction deviceAction)
    {
        this.deviceAction = deviceAction;
    }

    @Override
    public boolean isValid()
    {
        if(controllerName != null && Functions.isControllerPortValid(controllerPort)
                && Functions.isControllerSlotValid(controllerSlot) && device != null
                && deviceAction != null && deviceAction != StorageDeviceAction.INVALID_ACTION
                && (deviceAction == StorageDeviceAction.DETACH  || Functions.isUUIDValid(mediumId)))
            return true;
        else
            return false;
    }
}
